package com.huawei.app.demo.util;

import java.util.ArrayList;
import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

/**
 * 切换默认Launcher工具类
 *
 * @since 2021-05-10
 */
public class LauncherUtils {

    private static final String TAG = LauncherUtils.class.getSimpleName();

    /**
     * 清除默认Launcher应用
     * @param context
     */
    private static void clearDefaultLauncherApps(Context context) {
        List<IntentFilter> intentList = new ArrayList<IntentFilter>();
        List<ComponentName> componentNameList = new ArrayList<ComponentName>();

        PackageManager packageManager = context.getPackageManager();
        packageManager.getPreferredActivities(intentList, componentNameList, null);

        try {
            for (int i=0; i<intentList.size(); i++) {
                IntentFilter intentFilter = intentList.get(i);
                if (intentFilter.hasAction(Intent.ACTION_MAIN) && intentFilter.hasCategory(Intent.CATEGORY_HOME)) {
                    String pkgName = componentNameList.get(i).getPackageName();
                    packageManager.clearPackagePreferredActivities(pkgName);
                    LogUtils.getInstance().i(TAG, "清空Launcher: " + pkgName);
                }
            }
        } catch (Exception e) {
            LogUtils.getInstance().e(TAG, "clear default launcher failed", e);
        }
    }

    /**
     * 查询所有Launcher应用
     * @param context
     */
    private static List<ResolveInfo> getAllLauncherApps(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);

        PackageManager packageManager = context.getPackageManager();
        return packageManager.queryIntentActivities(intent, 0);
    }

    /**
     * 设置默认Launcher
     * @param context
     * @param packageName
     * @param mainActivityName
     * @return
     */
    public static boolean setDefaultLauncher(Context context, String packageName, String mainActivityName) {
        clearDefaultLauncherApps(context);
        List<ResolveInfo> allLauncherList = getAllLauncherApps(context);
        LogUtils.getInstance().i(TAG, "设备上安装的Launcher个数: " + allLauncherList.size());

        ComponentName[] componentNameSet = new ComponentName[allLauncherList.size()];
        ComponentName launcher = new ComponentName(packageName, mainActivityName);

        int defaultMatchLauncher = 0;
        for (int i=0; i<allLauncherList.size(); i++) {
            ResolveInfo resolveInfo = allLauncherList.get(i);
            componentNameSet[i] = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
            LogUtils.getInstance().i(TAG, "索引:" + i + ", activityName:" + resolveInfo.activityInfo.name);
            if (launcher.equals(componentNameSet[i])) {
                defaultMatchLauncher = resolveInfo.match;
                LogUtils.getInstance().i(TAG, "索引:" + i + ", match: " + resolveInfo.match);
            }
        }

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
        intentFilter.addCategory(Intent.CATEGORY_HOME);
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            context.getPackageManager().addPreferredActivity(intentFilter, defaultMatchLauncher, componentNameSet, launcher);
            LogUtils.getInstance().i(TAG, "设置Launcher: " + launcher);
            return true;
        } catch (Exception e) {
            LogUtils.getInstance().e(TAG, "set default launcher failed", e);
        }
        return false;
    }
}
